﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  public abstract class BufferConfiguration
  {
    #region Constructor

    internal BufferConfiguration()
    {
    }

    #endregion

    // This method is not intended to be called externally. 
    // It has been made internal so that it is not visible
    // publicly.
    internal abstract void Initialize( BufferInfo bufferInfo );

    // This method is not intended to be called externally. 
    // It has been made internal so that it is not visible
    // publicly.
    internal abstract int GetActivePreFetchSize( BufferInfo bufferInfo );

    // This method is not intended to be called externally. 
    // It has been made internal so that it is not visible
    // publicly.
    internal abstract int GetPassivePreFetchSize( BufferInfo bufferInfo );

    // This method is not intended to be called externally. 
    // It has been made internal so that it is not visible
    // publicly.
    internal abstract int GetBufferCapacity( BufferInfo bufferInfo );

    internal static void Prepare( BufferConfiguration configuration, BufferInfo bufferInfo )
    {
      Debug.Assert( ( configuration != null ) && ( bufferInfo != null ) );

      // Make sure the buffer info has been initialized by the same BufferConfiguration.
      if( ( bufferInfo.BufferConfiguration != null ) &&
          ( bufferInfo.BufferConfiguration != configuration ) )
      {
        bufferInfo.Reset();
      }

      // Make sure the BufferInfo gets initialized.
      if( !bufferInfo.IsInitialized )
      {
        configuration.Initialize( bufferInfo );

        bufferInfo.IsInitialized = true;
        bufferInfo.BufferConfiguration = configuration;
      }
    }

    internal int FindActivePreFetchSize( BufferInfo bufferInfo )
    {
      Debug.Assert( bufferInfo != null );

      int value = this.GetActivePreFetchSize( bufferInfo );
      int[] range = bufferInfo.ActivePreFetchingSizeRange;

      return BufferConfiguration.Clamp( value, range[ 0 ], range[ 1 ] );
    }

    internal int FindPassivePreFetchSize( BufferInfo bufferInfo )
    {
      Debug.Assert( bufferInfo != null );

      int value = this.GetPassivePreFetchSize( bufferInfo );
      int[] range = bufferInfo.PassivePreFetchingSizeRange;

      return BufferConfiguration.Clamp( value, range[ 0 ], range[ 1 ] );
    }

    internal int FindBufferCapacity( BufferInfo bufferInfo )
    {
      Debug.Assert( bufferInfo != null );

      int value = this.GetBufferCapacity( bufferInfo );
      int[] range = bufferInfo.CapacityRange;

      return BufferConfiguration.Clamp( value, range[ 0 ], range[ 1 ] );
    }

    private static int Clamp( int value, int minimum, int maximum )
    {
      Debug.Assert( minimum <= maximum );

      if( value < minimum )
      {
        return minimum;
      }
      else if( value > maximum )
      {
        return maximum;
      }
      else
      {
        return value;
      }
    }
  }
}
